package com.cxc.java8;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class FilterApple {

    @FunctionalInterface
    public interface AppleFilter{
        boolean  filter(Apple apple);
    }

    public static class GreenApple implements AppleFilter{
        @Override
        public boolean filter(Apple apple) {
            return apple.getColor().equals("green") && apple.getWeight() <= 130;
        }
    }

    public  static List<Apple> findGreeApple(List<Apple> apples,AppleFilter filter) {
        ArrayList<Apple> applesList = new ArrayList<>();
        for (Apple apple : apples) {
            if (filter.filter(apple)) {
                applesList.add(apple);
            }
        }
        return  applesList;
    }


    public  static List<Apple>  getGreenApple(List<Apple> apples){
        List<Apple> list = new ArrayList<>();
        for (Apple apple: apples){
            if("green".equals(apple.getColor())){
                list.add(apple);
            }
        }
        return list;
    }


    public static void main(String[] args) {
        List<Apple> apples = Arrays.asList(new Apple("green", 150), new Apple("green", 120),
                new Apple("red", 130), new Apple("green", 125));

        /*List<Apple> greenApples = getGreenApple(apples);
        System.out.println(greenApples);*/

      /*  List<Apple> result = findGreeApple(apples, new GreenApple());
        System.out.println(result);*/

       /*List<Apple> greeApple = findGreeApple(apples, new AppleFilter() {
            @Override
            public boolean filter(Apple apple) {
                return "green".equals(apple.getColor());
            }
        });
        System.out.println(greeApple);*/

        //使用 lambda 表示


       List<Apple> greeApple = findGreeApple(apples,apple -> {
           return "green".equals(apple.getColor());
       });
        System.out.println(greeApple);




       /* new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName());
            }
        }).start();*/

       new Thread(()->{
           System.out.println(Thread.currentThread().getName());
       }).start();




    }
}
